﻿var $$ = function(id){
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
	create: function(){
		return function(){
			this.initialize.apply(this, arguments);
		}
	}
}

var Extend = function(destination, source){
	for(var property in source){
		destination[property] = source[property];
	}
	return destination;
}

var Calendar = Class.create();
Calendar.prototype = {
	initialize: function(container, options){
		this.container = $$(container);
		this.days = [];
		
		this.setOptions(options);
		
		this.year = this.options.year || new Date().getFullYear();
		this.month = this.options.month || new Date().getMonth() + 1;
		this.selectDay = this.options.selectDay ? new Date(this.options.selectDay) : null;
		this.onSelectDay = this.options.onSelectDay;
		this.onToday = this.options.onToday;
		this.onFinish = this.options.onFinish;
		
		this.draw();
	},
	//设置默认属性
	setOptions: function(options){
		this.options = {//默认值
			year : 0,          //显示年
			month : 0,         //显示月
			selectDay : null,  //选择日期
			onSelectDay : function(){}, //在选择日期触发
			onToday : function(){},     //在当天日期触发
			onFinish : function(){}     //日历画完后触发
		}
		Extend(this.options, options || {});
	},
	//当前月
	nowMonth: function(){
		this.preDraw(new Date());
	},
	//上一月
	preMonth: function(){
		this.preDraw(new Date(this.year, this.month-2, 1));
	},
	//下一月
	nextMonth: function(){
		this.preDraw(new Date(this.year, this.month, 1));
	},
	//上一年
	preYear: function(){
		this.preDraw(new Date(this.year-1, this.month-1, 1));
	},
	//下一年
	nextYear: function(){
		this.preDraw(new Date(this.year+1, this.month-1, 1));
	},
	//根据日期画日历
	preDraw: function(date){
		//再设置属性
		this.year = date.getFullYear();
		this.month = date.getMonth() + 1;
		//重新画日历
		this.draw();
	},
	//画日历
	draw: function(){
		//用来保存日期列表
		var arr = [];
		//用当月第一天在一周中的日期值作为当月离第一天的天数
		for(var i=1, firstDay = new Date(this.year, this.month-1, 1).getDay(); i<=firstDay; i++){
		    arr.push(0);
		}
		//用当月最后一天在一个月中的日期值作为当月的天数
		for(var i=1, monthDay = new Date(this.year, this.month, 0).getDate(); i<=monthDay; i++){
			arr.push(i);
		}
		//请空难原来的日期对象列表
		this.days = [];
		//插入日期
		var frag = document.createDocumentFragment();
		while(arr.length){
			//每个星期插入一个tr
			var row = document.createElement('tr');
			//每个星期有7天
			for(var i=1; i<=7; i++){
			    var cell = document.createElement('td');
				cell.innerHTML = '&nbsp;';
				if(arr.length){
					var d = arr.shift();
					if(d){
						cell.innerHTML = d;
						this.days[d] = cell;
						var on = new Date(this.year, this.month-1, d);
						//判断是否今日
						this.isSame(on, new Date()) && this.onToday(cell);
						//判断是否选择日期
						this.selectDay && this.isSame(on, this.selectDay) && this.onSelectDay(cell);
					}
				}
				row.appendChild(cell);
			}
			frag.appendChild(row);
		}
		//先清空内容再插入(ie的table不能用innerHTML)
		while(this.container.hasChildNodes()){
			this.container.removeChild(this.container.firstChild);
		}
		this.container.appendChild(frag);
		//附加程序
		this.onFinish();
	},
	//判断是否同一日
	isSame: function(d1, d2){
		return (d1.getFullYear() == d2.getFullYear() &&
				d1.getMonth() == d2.getMonth() &&
				d1.getDate() == d2.getDate());
	}
	
	
	
	
}